package mi.payment.admin;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.widget.Toast;

import com.sunmi.pay.hardware.aidlv2.AidlConstantsV2;
import com.sunmi.pay.hardware.aidlv2.bean.ApduRecvV2;
import com.sunmi.pay.hardware.aidlv2.bean.ApduSendV2;

import java.security.KeyPair;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Random;

import lainey.ui.utils.LogUtil;
import mi.payment.Constant;
import mi.payment.MyApplication;
import mi.payment.R;
import mi.payment.bean.db.TradeData;
import mi.payment.constants.Params;
import mi.payment.db.dao.PayDetail;
import mi.payment.ui.base.ParentActivity;
import mi.payment.utils.AllianceUtil;
import mi.payment.utils.AsciiUtil;
import mi.payment.utils.ByteUtil;
import mi.payment.utils.EncryptUtil;
import mi.payment.utils.RSAUtil;
import mi.payment.utils.ShaUtil;
import mi.payment.utils.transaction.RKSTask;
import sunmi.payment.ui.dialog.LoadDialog;

public class RKS {

    private String pin = "250707";
    private String acquireID = "abmB".toUpperCase();
    private String vendorID = "iPay88".toUpperCase();

    public static String mSessionKey;
    public static String mSessionKcv;
    public static String mSessionAppID;
    public static String mEncryptedData;

    private KeyPair mKeyPair;
    private ParentActivity mActivity;

    private String meTSK1;
    private String mRndB1B2;
    private byte[] mTSKBytes;
    private byte[] mAPPKEYBytes;
    private byte[] mRndA1B2B1A2Bytes;

    RKS(ParentActivity activity) {
        mActivity = activity;
    }

    void operate() {
        try {
            mKeyPair = RSAUtil.generateRSAKeyPair();

            boolean result = selectAID();
            if (result == false) {
                mActivity.dismissLoadingDialog();
                return;
            }

            result = establishSecureChannel();
            if (result == false) {
                mActivity.dismissLoadingDialog();
                return;
            }

            result = initializeRsaPublicKey(true);
            if (result == false) {
                mActivity.dismissLoadingDialog();
                return;
            }

            result = initializeRsaPublicKey(false);
            if (result == false) {
                mActivity.dismissLoadingDialog();
                return;
            }

            result = requestTerminalSessionKey(true);
            if (result == false) {
                mActivity.dismissLoadingDialog();
                return;
            }

            result = requestTerminalSessionKey(false);
            if (result == false) {
                mActivity.dismissLoadingDialog();
                return;
            }

            result = getAppKey();
            if (result == false) {
                mActivity.dismissLoadingDialog();
                return;
            }

            result = initializeMutualAuthentication();
            if (result == false) {
                mActivity.dismissLoadingDialog();
                return;
            }

            result = mutualAuthentication();
            if (result == false) {
                mActivity.dismissLoadingDialog();
                return;
            }

            result = verifyPIN();
            if (result == false) {
                mActivity.dismissLoadingDialog();
                return;
            }

            result = requestSession();
            if (result == false) {
                mActivity.dismissLoadingDialog();
                return;
            }

            parametersDownload();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressLint("StaticFieldLeak")
    private void parametersDownload() {
        RKSTask task = new RKSTask(mActivity) {

            @Override
            protected void onPostExecuteImpl(TradeData tradeData) {
                PayDetail resPay = tradeData.getPayDetail();
                boolean equals = TextUtils.equals("00", resPay.tradeAnswerCode);
                if (equals) {
                    try {
                        mEncryptedData = mEncryptedData.substring(20);
                        equals = authSession(mEncryptedData);
                        if (equals) {
                            Toast.makeText(mActivity, R.string.merchant_safe_code_success, Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(mActivity, R.string.merchant_safe_code_failed, Toast.LENGTH_SHORT).show();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

        };
        TradeData tradeData = Params.newTradeData();
        LoadDialog loadDialog = mActivity.getLoadingDialog(R.string.load_downloading);
        task.setLoadDialog(loadDialog);
        task.execute(tradeData);
    }

    /**
     * 选择App ID
     */
    private boolean selectAID() throws Exception {
        ApduSendV2 send = new ApduSendV2();
        send.command = ByteUtil.hexStr2Bytes("00A4040C");
        send.lc = Short.parseShort("7");
        send.le = Short.parseShort("255");
        send.dataIn = ByteUtil.hexStr2Bytes("A0000047530501");

        ApduRecvV2 rec = sendCommand(send);
        String swa = ByteUtil.byte2HexStr(rec.swa);
        return TextUtils.equals("90", swa);
    }

    /**
     * 建立安全通道
     */
    private boolean establishSecureChannel() throws Exception {
        ApduSendV2 send = new ApduSendV2();
        send.command = ByteUtil.hexStr2Bytes("00470002");
        send.lc = Short.parseShort("16");
        send.le = Short.parseShort("255");

        String rndX = generateRandom(8);
        LogUtil.e(Constant.TAG, "rndX: " + rndX);

        String cryptogramX = getEstablishSecureChannelData(rndX);
        send.dataIn = ByteUtil.hexStr2Bytes(rndX + cryptogramX);

        ApduRecvV2 rec = sendCommand(send);
        String swa = ByteUtil.byte2HexStr(rec.swa);
        return TextUtils.equals("90", swa);
    }

    /**
     * 初始化RSA公钥
     */
    private boolean initializeRsaPublicKey(boolean first) throws Exception {
        RSAPublicKey rsaPublicKey = (RSAPublicKey) mKeyPair.getPublic();
        byte[] modulusBytes = rsaPublicKey.getModulus().toByteArray();
        String modulus = ByteUtil.bytes2HexStr(modulusBytes);
        LogUtil.e(Constant.TAG, "modulus: " + modulus);
        LogUtil.e(Constant.TAG, "rsaPublicKey: " + rsaPublicKey);

        ApduSendV2 send = new ApduSendV2();
        if (first) {
            send.command = ByteUtil.hexStr2Bytes("00340001");
            String dataIn = modulus.substring(2, 258);
            LogUtil.e(Constant.TAG, "send rsa pub 1: " + dataIn);
            send.dataIn = ByteUtil.hexStr2Bytes(dataIn);
        } else {
            send.command = ByteUtil.hexStr2Bytes("00340002");
            String dataIn = modulus.substring(258, 514);
            LogUtil.e(Constant.TAG, "send rsa pub 2 " + dataIn);
            send.dataIn = ByteUtil.hexStr2Bytes(dataIn);
        }
        send.lc = Short.parseShort("128");
        send.le = Short.parseShort("255");

        ApduRecvV2 rec = sendCommand(send);
        String swa = ByteUtil.byte2HexStr(rec.swa);
        return TextUtils.equals("90", swa);
    }

    private boolean requestTerminalSessionKey(boolean first) throws Exception {
        ApduSendV2 send = new ApduSendV2();
        if (first) {
            send.command = ByteUtil.hexStr2Bytes("00480001");
        } else {
            send.command = ByteUtil.hexStr2Bytes("00480002");
        }
        ApduRecvV2 rec = sendCommand(send);
        String swa = ByteUtil.byte2HexStr(rec.swa);
        if (rec.outlen > 0) {
            if (first) {
                meTSK1 = ByteUtil.bytes2HexStr(rec.outData).substring(0, rec.outlen * 2);
                LogUtil.e(Constant.TAG, "meTSK1: " + meTSK1);
                return TextUtils.equals("90", swa);
            } else {
                String meTSK2 = ByteUtil.bytes2HexStr(rec.outData).substring(0, rec.outlen * 2);
                LogUtil.e(Constant.TAG, "meTSK2: " + meTSK2);

                String eTSK = meTSK1 + meTSK2.substring(0, 256);
                LogUtil.e(Constant.TAG, "eTSK: " + eTSK);
                byte[] dataIn = ByteUtil.hexStr2Bytes(eTSK);

                int length = meTSK2.length();
                String kcv = meTSK2.substring(256, length);
                LogUtil.e(Constant.TAG, "kcv: " + kcv);

                RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) mKeyPair.getPrivate();
                byte[] bytes = RSAUtil.decryptByPrivateKey(rsaPrivateKey, dataIn);

                mTSKBytes = new byte[16];
                System.arraycopy(bytes, 0, mTSKBytes, 0, 16);

                String mTSKStr = ByteUtil.bytes2HexStr(mTSKBytes);
                LogUtil.e(Constant.TAG, "mTSKStr: " + mTSKStr);

                byte[] data = new byte[8];
                byte[] calcKCV_bytes = EncryptUtil.encrypt3Des(mTSKBytes, data);
                String calcKCV = ByteUtil.bytes2HexStr(calcKCV_bytes).substring(0, 4);
                LogUtil.e(Constant.TAG, "calc kcv: " + calcKCV);

                return TextUtils.equals(kcv, calcKCV);
            }
        }
        return false;
    }

    /**
     * 获取App Key
     */
    private boolean getAppKey() throws Exception {
        ApduSendV2 send = new ApduSendV2();
        send.command = ByteUtil.hexStr2Bytes("00460000");
        ApduRecvV2 rec = sendCommand(send);
        if (rec.outlen > 0) {
            String temp = ByteUtil.bytes2HexStr(rec.outData).substring(0, rec.outlen * 2);
            String eAppKey = temp.substring(0, 48);
            String eAppKey_kcv = temp.substring(48, 52);
            LogUtil.e(Constant.TAG, "eAppKey: " + eAppKey);
            LogUtil.e(Constant.TAG, "appKey_kcv: " + eAppKey_kcv);

            eAppKey = eAppKey.substring(0, 32);
            byte[] data = ByteUtil.hexStr2Bytes(eAppKey);
            mAPPKEYBytes = EncryptUtil.decrypt3Des(mTSKBytes, data);
            String appKey = ByteUtil.bytes2HexStr(mAPPKEYBytes);
            LogUtil.e(Constant.TAG, "appKey: " + appKey);

            data = new byte[8];
            byte[] calcKCV_bytes = EncryptUtil.encrypt3Des(mAPPKEYBytes, data);
            String calcKCV = ByteUtil.bytes2HexStr(calcKCV_bytes).substring(0, 4);
            LogUtil.e(Constant.TAG, "calc kcv: " + calcKCV);

            return TextUtils.equals(eAppKey_kcv, calcKCV);
        }
        return false;
    }

    /**
     * 初始化双向认证通道
     */
    private boolean initializeMutualAuthentication() throws Exception {
        ApduSendV2 send = new ApduSendV2();
        send.command = ByteUtil.hexStr2Bytes("00430000");
        send.lc = Short.parseShort("8");
        send.le = Short.parseShort("255");

        String rndA1A2 = generateRandom(8);
        LogUtil.e(Constant.TAG, "rndA1A2: " + rndA1A2);

        byte[] data = ByteUtil.hexStr2Bytes(rndA1A2);
        byte[] eRndA1A2_bytes = EncryptUtil.encrypt3Des(mAPPKEYBytes, data);
        String eRndA1A2 = ByteUtil.bytes2HexStr(eRndA1A2_bytes);
        LogUtil.e(Constant.TAG, "eRndA1A2: " + eRndA1A2);

        send.dataIn = eRndA1A2_bytes;
        ApduRecvV2 rec = sendCommand(send);
        if (rec.outlen > 0) {
            String temp = ByteUtil.bytes2HexStr(rec.outData).substring(0, rec.outlen * 2);
            String eRndA1B2B1A2 = temp.substring(0, 32);
            LogUtil.e(Constant.TAG, "eRndA1B2B1A2: " + eRndA1B2B1A2);

            // Notice encrypt
            mRndA1B2B1A2Bytes = EncryptUtil.encrypt3Des(mAPPKEYBytes, rec.outData);
            String rndA1B2B1A2 = ByteUtil.bytes2HexStr(mRndA1B2B1A2Bytes);
            LogUtil.e(Constant.TAG, "rndA1B2B1A2: " + rndA1B2B1A2);

            String recRndA1A2 = rndA1B2B1A2.substring(0, 8) + rndA1B2B1A2.substring(24, 32);
            LogUtil.e(Constant.TAG, "recRndA1A2: " + recRndA1A2);

            mRndB1B2 = rndA1B2B1A2.substring(16, 24) + rndA1B2B1A2.substring(8, 16);
            LogUtil.e(Constant.TAG, "mRndB1B2: " + mRndB1B2);

            return TextUtils.equals(rndA1A2, recRndA1A2);
        }
        return false;
    }

    /**
     * 相互认证
     */
    private boolean mutualAuthentication() throws Exception {
        ApduSendV2 send = new ApduSendV2();
        send.command = ByteUtil.hexStr2Bytes("00440000");
        send.lc = Short.parseShort("8");
        send.le = Short.parseShort("255");

        byte[] data = ByteUtil.hexStr2Bytes(mRndB1B2);
        byte[] eRndB1B2_bytes = EncryptUtil.encrypt3Des(mAPPKEYBytes, data);
        String eRndB1B2 = ByteUtil.bytes2HexStr(eRndB1B2_bytes);
        LogUtil.e(Constant.TAG, "eRndB1B2: " + eRndB1B2);

        send.dataIn = eRndB1B2_bytes;

        ApduRecvV2 rec = sendCommand(send);
        LogUtil.e(Constant.TAG, "相互认证: " + rec.outlen);
        String swa = ByteUtil.byte2HexStr(rec.swa);
        return TextUtils.equals("90", swa);
    }

    /**
     * 校验PIN
     */
    private boolean verifyPIN() throws Exception {
        ApduSendV2 send = new ApduSendV2();
        send.command = ByteUtil.hexStr2Bytes("00200000");
        send.lc = Short.parseShort("8");
        send.le = Short.parseShort("255");

        byte[] pin_bytes = pin.getBytes();
        String pin = ByteUtil.bytes2HexStr(pin_bytes);
        LogUtil.e(Constant.TAG, "pin: " + pin);

        byte[] data = new byte[8];
        System.arraycopy(pin_bytes, 0, data, 0, pin_bytes.length);
        data[6] = (byte) 0x80;
        data[7] = (byte) 0x00;
        byte[] ePin_bytes = EncryptUtil.encrypt3Des(mRndA1B2B1A2Bytes, data);
        String ePin = ByteUtil.bytes2HexStr(ePin_bytes);
        LogUtil.e(Constant.TAG, "ePin: " + ePin);

        send.dataIn = ePin_bytes;

        ApduRecvV2 rec = sendCommand(send);
        LogUtil.e(Constant.TAG, "校验PIN: " + rec.outlen);
        String swa = ByteUtil.byte2HexStr(rec.swa);
        return TextUtils.equals("90", swa);
    }

    /**
     * 请求会话
     */
    private boolean requestSession() throws Exception {
        ApduSendV2 send = new ApduSendV2();
        send.command = ByteUtil.hexStr2Bytes("00410000");
        send.lc = Short.parseShort("40");
        send.le = Short.parseShort("255");

        byte[] dataIn = new byte[40];

        byte[] bytes = acquireID.getBytes();
        System.arraycopy(bytes, 0, dataIn, 0, bytes.length);

        bytes = vendorID.getBytes();
        System.arraycopy(bytes, 0, dataIn, 20, bytes.length);

        String requestData = ByteUtil.bytes2HexStr(dataIn);
        LogUtil.e(Constant.TAG, "requestData: " + requestData);

        send.dataIn = dataIn;

        ApduRecvV2 rec = sendCommand(send);
        if (rec.outlen > 0) {
            byte[] data = new byte[20];
            System.arraycopy(rec.outData, 0, data, 0, 20);
            mSessionAppID = ByteUtil.bytes2HexStr(data);
            LogUtil.e(Constant.TAG, "mSessionAppID: " + mSessionAppID);

            data = new byte[16];
            System.arraycopy(rec.outData, 20, data, 0, 16);
            mSessionKey = ByteUtil.bytes2HexStr(data);
            LogUtil.e(Constant.TAG, "mSessionKey: " + mSessionKey);

            data = new byte[2];
            System.arraycopy(rec.outData, 36, data, 0, 2);
            mSessionKcv = ByteUtil.bytes2HexStr(data);
            LogUtil.e(Constant.TAG, "mSessionKcv: " + mSessionKcv);
        }
        String swa = ByteUtil.byte2HexStr(rec.swa);
        return TextUtils.equals("90", swa);
    }

    /**
     * 会话认证
     */
    private boolean authSession(String hexData) throws Exception {
        ApduSendV2 send = new ApduSendV2();
        send.command = ByteUtil.hexStr2Bytes("00490000");
        send.lc = Short.parseShort(hexData.length() / 2 + "");
        send.le = Short.parseShort("255");

        LogUtil.e(Constant.TAG, "authSession: " + hexData);
        send.dataIn = ByteUtil.hexStr2Bytes(hexData);

        ApduRecvV2 rec = sendCommand(send);
        if (rec.outlen > 0) {
            byte[] decryptBytes = new byte[rec.outlen];
            System.arraycopy(rec.outData, 0, decryptBytes, 0, rec.outlen);
            byte[] clearBytes = EncryptUtil.decrypt3Des(mRndA1B2B1A2Bytes, decryptBytes);
            String temp = ByteUtil.bytes2HexStr(clearBytes);
            temp = AllianceUtil.decode250(temp);
            LogUtil.e(Constant.TAG, "Keys: " + temp);

            String ksnStr = temp.substring(0, 40);
            LogUtil.e(Constant.TAG, "ksnStr: " + ksnStr);
            ksnStr = AsciiUtil.asciiToString(ksnStr);
            LogUtil.e(Constant.TAG, "ksnStr: " + ksnStr);

            String separator = temp.substring(40, 42);
            LogUtil.e(Constant.TAG, "separator: " + separator);

            String iPekStr = temp.substring(42, 74);
            LogUtil.e(Constant.TAG, "iPekStr: " + iPekStr);

            separator = temp.substring(74, 76);
            LogUtil.e(Constant.TAG, "separator: " + separator);

            String iPekKcvStr = temp.substring(76, 84);
            LogUtil.e(Constant.TAG, "iPekKcvStr: " + iPekKcvStr);

            separator = temp.substring(84, 86);
            LogUtil.e(Constant.TAG, "separator: " + separator);

            String kidStr = temp.substring(86, 98);
            LogUtil.e(Constant.TAG, "kidStr: " + kidStr);

            separator = temp.substring(98, 100);
            LogUtil.e(Constant.TAG, "separator: " + separator);

            String dekStr = temp.substring(100, 132);
            LogUtil.e(Constant.TAG, "dekStr: " + dekStr);

            separator = temp.substring(132, 134);
            LogUtil.e(Constant.TAG, "separator: " + separator);

            String dekKcvStr = temp.substring(134, 142);
            LogUtil.e(Constant.TAG, "dekKcvStr: " + dekKcvStr);

            byte[] ksnBytes = ByteUtil.hexStr2Bytes(ksnStr);
            byte[] keyValueBytes = ByteUtil.hexStr2Bytes(iPekStr);
            byte[] checkValueBytes = ByteUtil.hexStr2Bytes(iPekKcvStr);
            int result = MyApplication.getInstance().mSecurityOptV2.saveKeyDukpt(AidlConstantsV2.Security.KEY_TYPE_DUPKT_IPEK,
                    keyValueBytes, null, ksnBytes, AidlConstantsV2.Security.KEY_ALG_TYPE_3DES, Constant.indexDuKpt);
            LogUtil.e(Constant.TAG, "save duKpt result: " + result);
            return result == 0;
        }
        return false;
    }

    private String generateRandom(int num) {
        String result = "";
        for (int i = 0; i < num; i++) {
            int var = new Random().nextInt(9);
            result += "3" + var;
        }
        return result;
    }

    private String getEstablishSecureChannelData(String rndX) throws Exception {
        byte[] acquireIdBytes = new byte[20];
        byte[] bytes = acquireID.getBytes();
        System.arraycopy(bytes, 0, acquireIdBytes, 0, bytes.length);

        String acquireId = ByteUtil.bytes2HexStr(acquireIdBytes);
        LogUtil.e(Constant.TAG, "acquireId: " + acquireId);

        byte[] vendorIdBytes = new byte[20];
        bytes = vendorID.getBytes();
        System.arraycopy(bytes, 0, vendorIdBytes, 0, bytes.length);

        String vendorId = ByteUtil.bytes2HexStr(vendorIdBytes);
        LogUtil.e(Constant.TAG, "vendorId: " + vendorId);

        byte[] rndXBytes = ByteUtil.hexStr2Bytes(rndX);

        byte[] blockA = new byte[10];
        System.arraycopy(acquireIdBytes, 0, blockA, 0, 10);
        String temp = ByteUtil.bytes2HexStr(blockA);
        LogUtil.e(Constant.TAG, "blockA: " + temp);

        byte[] blockB = new byte[10];
        System.arraycopy(acquireIdBytes, 10, blockB, 0, 10);
        temp = ByteUtil.bytes2HexStr(blockB);
        LogUtil.e(Constant.TAG, "blockB: " + temp);

        byte[] blockC = new byte[10];
        System.arraycopy(vendorIdBytes, 0, blockC, 0, 10);
        temp = ByteUtil.bytes2HexStr(blockC);
        LogUtil.e(Constant.TAG, "blockC: " + temp);

        byte[] blockD = new byte[10];
        System.arraycopy(vendorIdBytes, 10, blockD, 0, 10);
        temp = ByteUtil.bytes2HexStr(blockD);
        LogUtil.e(Constant.TAG, "blockD: " + temp);

        // BlockAD = XOR BlockA & BlockD
        byte[] blockAD = new byte[blockA.length];
        for (int i = 0; i < blockA.length; i++) {
            int var = blockA[i] ^ blockD[i];
            blockAD[i] = (byte) var;
        }
        temp = ByteUtil.bytes2HexStr(blockAD);
        LogUtil.e(Constant.TAG, "blockAD: " + temp);

        // BlockBC = XOR BlockB & BlockC
        byte[] blockBC = new byte[blockB.length];
        for (int i = 0; i < blockB.length; i++) {
            int var = blockB[i] ^ blockC[i];
            blockBC[i] = (byte) var;
        }
        temp = ByteUtil.bytes2HexStr(blockBC);
        LogUtil.e(Constant.TAG, "blockBC: " + temp);

        // BlockADBC = Joining BlockAD & BlockBC
        byte[] blockADBC = new byte[blockAD.length + blockBC.length];
        System.arraycopy(blockAD, 0, blockADBC, 0, blockAD.length);
        System.arraycopy(blockBC, 0, blockADBC, blockAD.length, blockBC.length);
        temp = ByteUtil.bytes2HexStr(blockADBC);
        LogUtil.e(Constant.TAG, "blockADBC: " + temp);

        // blockF = XOR BlockADBC & RndX
        byte[] blockF = new byte[blockADBC.length];
        System.arraycopy(rndXBytes, 0, blockF, 0, rndXBytes.length);
        for (int i = 0; i < blockF.length; i++) {
            blockF[i] ^= blockADBC[i];
        }
        temp = ByteUtil.bytes2HexStr(blockF);
        LogUtil.e(Constant.TAG, "blockF: " + temp);

        // BlockG = Sha_1(BlockF)
        byte[] blockG = ShaUtil.sha_1(blockF);
        temp = ByteUtil.bytes2HexStr(blockG);
        LogUtil.e(Constant.TAG, "blockG: " + temp);

        // IAK = First 16 bytes of Block G
        byte[] IAKBytes = new byte[16];
        System.arraycopy(blockG, 0, IAKBytes, 0, 16);
        temp = ByteUtil.bytes2HexStr(IAKBytes);
        LogUtil.e(Constant.TAG, "IAK: " + temp);

        // Terminal generate cryptogramX
        // 3DES [IAK](RndX)CBC_NO PAD
        byte[] cryptogramX = EncryptUtil.encrypt3Des(IAKBytes, rndXBytes);
        temp = ByteUtil.bytes2HexStr(cryptogramX);
        LogUtil.e(Constant.TAG, "cryptogramX: " + temp);
        return temp;
    }

    private ApduRecvV2 sendCommand(ApduSendV2 send) throws Exception {
        ApduRecvV2 rec = new ApduRecvV2();
        int result = MyApplication.getInstance().mReadCardOptV2.apduCommand(AidlConstantsV2.CardType.IC.getValue(), send, rec);
        if (result == 0) {
            String temp = "SWA：" + ByteUtil.byte2HexStr(rec.swa);
            temp += " SWB：" + ByteUtil.byte2HexStr(rec.swb);
            if (rec.outlen > 0) {
                temp += " outData：" + ByteUtil.bytes2HexStr(rec.outData).substring(0, rec.outlen * 2);
            } else {
                temp += " outData：";
            }
            LogUtil.e(Constant.TAG, "rec: " + temp);
        }
        return rec;
    }


}
